<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js" type="application/javascript"
  ></script>
</head>
<body>
<div id="app">
  <img :src="imgPath" ref="imgRef" @error="onError">
</div>
</body>
<script>
  new Vue({
    data() {
      return {
        imgPath: 'http://localhost:8080/undefined'
      }
    },
    mounted() {
      // 方式1 - 通过ref监听dom元素
      /*      let that = this
            that.$refs.imgRef.onerror = function() {
              that.imgPath = 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg'
            }*/

      /*      this.$refs.imgRef.onerror = () => {
              this.imgPath = 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg'
            }*/
    },
    methods: {

      //方式二：通过v-on监听错误信息事件
      // onError() {
      //   this.imgPath = 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg'
      // }
    },
    el: '#app'
  })
</script>
</html>
